package merchant.space;

import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;


public class MyCollectionsGroupActivity extends TabGroupActivity {
	
	private Navigation navigation;
	private static final int SELECT_PHOTO = 200;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       MerchantActivity parent = (MerchantActivity)getParent();
       Log.d("CONFIRM DAD MERCHANT",parent.toString());
    	navigation = parent.getNavigation();
      
          startChildActivity("Collectioning Activity Group", new Intent(this,MyCollectionsActivity.class));
     //   startChildActivity("Browsing Activity Group", new Intent(this,BrowseCollectionsActivity.class));
    }
	

	public Navigation getNavigation()
	{
		return navigation;
	}
	
	public void backwarding()
	{
		boolean done = false;
		Log.d("BACKING","BACK");
		while(!done)
		{
			Activity current = getCurrentActivity();
			if (current.getClass()==MyCollectionsActivity.class) done=true;
			else
				current.finish();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  // Log.d("RESULT",":"+data.getExtras().get("myid").toString());
		 Log.d("eh1","eh3");
		 switch(requestCode) 
		 {
		 case CamControl.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
	//	if (requestCode == CamControl.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
	        if (resultCode == RESULT_OK) {
	        	Log.d("RESULT","nice");
	        	
	        	if (data==null) Log.d("NULL","n");
	        
	        	if(getCurrentActivity().getClass()==AddItemActivity.class)
	        		((AddItemActivity)getCurrentActivity()).afterPhoto();	       
	        	//	Uri uri = (Uri)data.getExtras().get(MediaStore.EXTRA_OUTPUT);
	      
	        } else if (resultCode == RESULT_CANCELED) {
	        	Log.d("RESULT","cancel");
	            // User cancelled the image capture
	        } else {
	        	Log.d("RESULT","fail");
	            // Image capture failed, advise user
	        }
	    
	        break;
	        
	       //super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

	 
	    case SELECT_PHOTO:
	        if(resultCode == RESULT_OK){  
	           // Uri selectedImage = imageReturnedIntent.getData();
	           Uri selectedImage = data.getData();
	           
	           String filemanagerstring = selectedImage.getPath();
	           	//MEDIA GALLERY
	           String selectedImagePath = getPath(selectedImage);

	           
	           
	            InputStream imageStream;
				try {
					imageStream = getContentResolver().openInputStream(selectedImage);
			
	            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
	            
	          
	            
	            Log.d("PAAAAAAATH",selectedImagePath);
	            if(getCurrentActivity().getClass()==AddItemActivity.class)
	        		((AddItemActivity)getCurrentActivity()).afterPick(selectedImagePath);	     
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        
	        break;
	    }
	}

		
		
	
	
	 @Override
	    public boolean onKeyDown (int keyCode, KeyEvent event)
	    {
	    	
		 Activity current = getCurrentActivity();
		 if (current.getClass()==MyCollectionsActivity.class)
		 
			 return current.onKeyDown(keyCode, event);
		 	
		 
		 else
			return super.onKeyDown(keyCode, event);
			
	    }
	    
//	    }
	    
	
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	     Activity current = getCurrentActivity();
	     
	     if (current.getClass()==MyCollectionsActivity.class)
	    	 return getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
	     
	     else
	     {
	    	// super(menu);
		    return true;
	     }
	        
	    }
	 
	 
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) {
		  Activity current = getCurrentActivity();
		     
		     if (current.getClass()==MyCollectionsActivity.class)
		    	 return getLocalActivityManager().getCurrentActivity().onOptionsItemSelected(item);
		     
		     else
		     {
		    	// super(menu);
			    return true;
		     }
		        
		    }
	 
	private int column_index;

	 public String getPath(Uri uri) {
		 String[] projection = { MediaColumns.DATA };
		 Cursor cursor = managedQuery(uri, projection, null, null, null);
		 column_index = cursor
		         .getColumnIndexOrThrow(MediaColumns.DATA);
		 cursor.moveToFirst();
		 //imagePath = 
		 cursor.getString(column_index);

		 return cursor.getString(column_index);
		 }

	
}
